﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace com.lib.events
{
    public delegate void EventCall(object param);
   public class EventLib
    {

        /// <summary>
        /// 数据处理
        /// </summary>
       private Dictionary<object, EventCall> oncedata = new Dictionary<object, EventCall>();

        private Dictionary<object, EventCall> data = new Dictionary<object, EventCall>();

        private List<EventData> calldata = new List<EventData>();

        /// <summary>
        /// 静态类
        /// </summary>
        private static EventLib instance = null;

        /// <summary>
        /// 示例
        /// </summary>
        public static EventLib Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new EventLib();
                }

                return instance;
            }

        }

        /// <summary>
        /// 注册事件
        /// </summary>
        /// <param name="id"></param>
        /// <param name="call"></param>
        /// <param name="once"></param>
        public void RegisterEvent(int id, EventCall call, bool once = false)
        {
            if (once)
            {
                if (data.ContainsKey(id))
                {
                    data[id] -= data[id];
                    data.Remove(id);
                }
                data.Add(id, call);
                return;
            }
            if (oncedata.ContainsKey(id))
            {
                oncedata[id] -= oncedata[id];
                oncedata.Remove(id);
            }
            oncedata.Add(id, call);

        }

        /// <summary>
        /// 移除事件
        /// </summary>
        /// <param name="id"></param>
        public void RemoveEvent(int id)
        {
            if (data.ContainsKey(id))
            {
                data[id] -= data[id];
                data.Remove(id);
            }
            if (oncedata.ContainsKey(id))
            {
                oncedata[id] -= oncedata[id];
                oncedata.Remove(id);
            }
        }

        /// <summary>
        /// 清理事件
        /// </summary>
        public void ClearEvent()
        {
            lock (oncedata)
            {
                List<object> key = new List<object>(oncedata.Keys);
                foreach (var m in key)
                {
                    oncedata[m] -= oncedata[m];
                }
                key = new List<object>(data.Keys);
                foreach (var m in key)
                {
                    data[m] -= data[m];
                }
                oncedata.Clear();
                data.Clear();
            }


        }


        /// <summary>
        /// 请调用此函数
        /// </summary>
        public void Update()
        {
            lock (calldata)
            {
                if (calldata.Count > 0)
                {
                    var obj = calldata[0];
                    obj.call(obj.o);
                    calldata.RemoveAt(0);
                }
            }


        }

        /// <summary>
        /// 发送事件
        /// </summary>
        /// <param name="id"></param>
        /// <param name="o"></param>
        public void SendEvent(int id, object o)
        {

            if (data.ContainsKey(id))
            {
                EventData e = new EventData();
                e.call = data[id];
                e.o = o;
                calldata.Add(e);
                return;
            }
            if (oncedata.ContainsKey(id))
            {
                EventData e = new EventData();
                e.call = oncedata[id];
                e.o = o;
                calldata.Add(e);
                data.Remove(id);
                return;
            }
        }
    }
   public class EventData
   {
       public EventCall call { get; set; }

       public object o { get; set; }
   }
}
